﻿Imports System.Data.OleDb

Partial Public Class showTags
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Dim tagCountSql As String = "SELECT TagsMaterialQuery.tagcount,TagMaterialJunction.TagId, Tags.TagName FROM(TagsMaterialQuery, Tags)WHERE TagsMaterialQuery.TagId=Tags.ID;"
        'Dim tagCountSql As String = "SELECT MaterialCollection.MaterialName, TagMaterialJunction.MaterialFkId, COUNT(*) AS test FROM MaterialCollection INNER JOIN TagMaterialJunction ON MaterialCollection.ID = TagMaterialJunction.MaterialFkId GROUP BY MaterialCollection.MaterialName, TagMaterialJunction.MaterialFkId;"
        Dim tagCountSql As String = "SELECT  Tags.TagName, Count(*)  as test FROM Tags INNER JOIN TagMaterialJunction ON Tags.ID = TagMaterialJunction.TagId GROUP BY Tags.TagName;"

        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings _
                    ("ConnectionString").ToString())
        conn.Open()
        Dim tagCountCmd As New OleDbCommand(tagCountSql, conn)
        Dim tagCountReader = tagCountCmd.ExecuteReader()
        Dim tagHash As New Hashtable
        Dim maxTagct As Integer = 0
        Dim minTagct As Integer = 0


        If tagCountReader.HasRows() Then
            While tagCountReader.Read()
                'Response.Write(tagCountReader("TagName") & "" & tagCountReader("test"))
                tagHash.Add(tagCountReader("TagName"), CInt(tagCountReader("test")))
                If minTagct = 0 Then minTagct = CInt(tagCountReader("test"))
                If minTagct > CInt(tagCountReader("test")) Then minTagct = CInt(tagCountReader("test"))
                If maxTagct < CInt(tagCountReader("test")) Then maxTagct = CInt(tagCountReader("test"))

            End While
        End If



        Dim fontCategory() As String = {"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"}

        Dim scaleunit As Decimal = (maxTagct - minTagct + 1) / Convert.ToDecimal(fontCategory.Length)

        Dim tagCloudhtml As String = ""
        Dim htmlPageItem As String = "showTagssub.aspx?"
        For Each tmpTag In tagHash
            Dim scalevalue As Integer = Math.Truncate((tmpTag.Value - minTagct) / scaleunit)
            tagCloudhtml = tagCloudhtml & String.Format("<A HREF = ""{0}"" style=""font-size:{1};""> {2} </A>", htmlPageItem & "TagName=" & tmpTag.key, fontCategory(scalevalue), tmpTag.key)
        Next

        MatTagContainer.Controls.Add(New LiteralControl(tagCloudhtml))

    End Sub

End Class